package com.hll.api.controller.kaola;

import com.hll.api.config.web.RespMessage;
import com.hll.api.entity.SouParm;
import com.hll.api.service.KaolaApi;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;

/**
 * 网易考拉接口
 *
 * @author 何龙
 * @create 2019-06-22 15:19
 */
@RestController
@RequestMapping("/api/kaola")
public class KaolaController {
    @Resource
    KaolaApi api;


    /**
     * @api {get} /api/kaola/index 获取首页信息
     * @apiVersion 1.0.0
     * @apiName index
     * @apiGroup kaola
     * @apiParam {int} pageNo 页码，从1开始
     * @apiParamExample {string} 请求示例:
     * pageNo=1
     * @apiSuccess {Number} status 状态码,0为接口访问成功
     * @apiSuccess {String} err 错误信息
     * @apiSuccess {Object} datas 成功时返回考拉接口数据
     */
    @GetMapping("/index")
    public RespMessage index(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo) {
        return api.getIndex( pageNo );
    }

    /**
     * @api {get} /api/kaola/index/top 获取首页头部广告
     * @apiVersion 1.0.0
     * @apiName indexTop
     * @apiGroup kaola
     * @apiSuccess {Number} status 状态码,0为接口访问成功
     * @apiSuccess {String} err 错误信息
     * @apiSuccess {Object} datas 成功时返回考拉接口数据
     * @apiSuccessExample {json} 成功示例：
     * {
     * "status": 0,
     * "err": "成功",
     * "datas": {
     * "image": "https://haitao.nos.netease.com/0be211f4-b01f-40ca-836b-7020633e6cccT1905171417_180_180.jpg",
     * "btnText": "下载领取",
     * "showLayerIcon": true,
     * "link": "https://activity.kaola.com/appdownload.html",
     * "floatImg": "https://haitao.nos.netease.com/6f3857c4-89cb-4a20-b1c2-8ee8e3b47477T1906120024_180_180.jpg",
     * "type": 1,
     * "title": "新人领1000元现金红包",
     * "scm": "v1.1001.1001_mt_15558361._.dt_1-f_14129909-g_47-res_363676-s_19754345-set_1588694400000-fa_154089-pi_170296-ml_15433048-isResource_1",
     * "floatText": "打开考拉",
     * "desc": "网易考拉 就像你的网上免税店"
     * }
     * }
     */
    @GetMapping("/index/top")
    public RespMessage indexTop() {
        return api.getIndexTop();
    }

    /**
     * @api {get} /api/kaola/index/sou 获取首页搜索框文字
     * @apiVersion 1.0.0
     * @apiName indexSou
     * @apiGroup kaola
     * @apiSuccess {Number} status 状态码,0为接口访问成功
     * @apiSuccess {String} err 错误信息
     * @apiSuccess {Object} datas 成功时返回考拉接口数据
     * @apiSuccessExample {json} 成功示例：
     * {
     * "status": 0,
     * "err": "成功",
     * "datas": {
     * "keyUrlInBox": null,
     * "keyInBox": "飞利浦电动牙刷",
     * "keyOutBox": [
     * {
     * "showName": null,
     * "name": "洗发水吕",
     * "isBold": false,
     * "url": null
     * },
     * {
     * "showName": null,
     * "name": "尤妮佳纸尿裤/尿不湿",
     * "isBold": false,
     * "url": null
     * },
     * {
     * "showName": null,
     * "name": "袋鼠洗发水",
     * "isBold": false,
     * "url": null
     * },
     * {
     * "showName": null,
     * "name": "卫生巾花王",
     * "isBold": false,
     * "url": null
     * },
     * {
     * "showName": null,
     * "name": "兰蔻粉水",
     * "isBold": false,
     * "url": null
     * },
     * {
     * "showName": null,
     * "name": "飞利浦电动牙刷头",
     * "isBold": false,
     * "url": null
     * },
     * {
     * "showName": null,
     * "name": "阿迪达斯男鞋",
     * "isBold": false,
     * "url": null
     * },
     * {
     * "showName": null,
     * "name": "斯凯奇女鞋",
     * "isBold": false,
     * "url": null
     * },
     * {
     * "showName": null,
     * "name": "太阳伞",
     * "isBold": false,
     * "url": null
     * },
     * {
     * "showName": null,
     * "name": "良品铺子零食",
     * "isBold": false,
     * "url": null
     * },
     * {
     * "showName": null,
     * "name": "施华洛世奇手链",
     * "isBold": false,
     * "url": null
     * },
     * {
     * "showName": null,
     * "name": "维生素b",
     * "isBold": false,
     * "url": null
     * },
     * {
     * "showName": null,
     * "name": "jm面膜",
     * "isBold": false,
     * "url": null
     * }
     * ],
     * "keyShowInBox": null
     * }
     * }
     */
    @GetMapping("/index/sou")
    public RespMessage indexSou() {
        return api.getIndexSou();
    }

    /**
     * @api {get} /api/kaola/index/sou/tips 搜索文字提示
     * @apiVersion 1.0.0
     * @apiName indexSouTips
     * @apiGroup kaola
     * @apiParam {string} key 搜索词
     * @apiParamExample {string} 请求示例:
     * key=手机
     * @apiSuccess {Number} status 状态码,0为接口访问成功
     * @apiSuccess {String} err 错误信息
     * @apiSuccess {Object} datas 成功时返回考拉接口数据
     */
    @GetMapping("/index/sou/tips")
    public RespMessage indexSouTips(String key) {
        if (key == null) {
            return RespMessage.error( "key不能为空" );
        }
        return api.getIndexSouTips( key );
    }


    /**
     * @api {post} /api/kaola/index/sou/goods 搜索商品(json参数)
     * @apiVersion 1.0.0
     * @apiName souGoods
     * @apiGroup kaola
     * @apiHeader {String} Content-Type application/json;charset=UTF-8
     * @apiParam {string} key 搜索词
     * @apiParamExample {json} 请求示例:
     * {
     * "desc": 1,
     * "filterList": [{
     * "hidden": false,
     * "itemId": 11,
     * "itemName": "考拉服务",
     * "itemType": 0,
     * "lower": -1,
     * "showLine": 0,
     * "upper": -1
     * }],
     * "hasMore": true,
     * "isMarket": false,
     * "key": "尤妮佳纸尿裤",
     * "marketCategory": "",
     * "pageNo": 1,
     * "pageSize": 20,
     * "searchType": "synonym",
     * "sortType": 0,
     * "t": 1561296188123
     * }
     * @apiSuccess {Number} status 状态码,0为接口访问成功
     * @apiSuccess {String} err 错误信息
     * @apiSuccess {Object} datas 成功时返回考拉接口数据
     */
    @PostMapping("/index/sou/goods")
    public RespMessage indexSouGoods(@RequestBody SouParm parm) {
        if (parm == null) {
            parm = new SouParm();
        }
        return api.getIndexSouGoods( parm );
    }

    /**
     * @api {post} /api/kaola/index/sou/good 搜索商品(表单参数)
     * @apiVersion 2.0.0
     * @apiName souGoods
     * @apiGroup kaola
     * @apiParam {string} key 搜索词
     * @apiParam {int} pageNo 页码
     * @apiParam {int} pageSize 每页商品数量
     * @apiParam {int} sortType 排序类别<br/>
     * 0:综合<br/>
     * 1:价格<br/>
     * 2:销量<br/>
     * 6:新品<br/>
     * @apiParam {int} desc 是否降序<br/>
     * 0:升序<br/>
     * 1:降序<br/>
     * @apiParamExample {json} 请求示例:
     * {
     * "desc": 1,
     * "hasMore": true,
     * "isMarket": false,
     * "key": "尤妮佳纸尿裤",
     * "marketCategory": "",
     * "pageNo": 1,
     * "pageSize": 20,
     * "searchType": "synonym",
     * "sortType": 0,
     * }
     * @apiSuccess {Number} status 状态码,0为接口访问成功
     * @apiSuccess {String} err 错误信息
     * @apiSuccess {Object} datas 成功时返回考拉接口数据
     */
    @PostMapping("/index/sou/good")
    public RespMessage indexSouGood(SouParm parm) {
        if (parm == null) {
            parm = new SouParm();
        }
        return api.getIndexSouGoods( parm );
    }

    /**
     * @api {get} /api/kaola/get/image 获取商品图片
     * @apiVersion 1.0.0
     * @apiName image
     * @apiGroup kaola
     * @apiParam {string} imgCode 图片代码
     * @apiParam {int} width 图片宽度 默认500
     * @apiParam {int} quality 图片质量，0-100,默认100
     * @apiParamExample {json} 请求示例:
     * {
     * "imgCode": "9d75bb06-2350-4077-8717-dea5c8ad022f",
     * "width": 200,
     * "quality": 50,
     * }
     * @apiSuccess {Number} status 状态码,0为接口访问成功
     * @apiSuccess {String} err 错误信息
     * @apiSuccess {Object} datas 成功时返回考拉接口数据
     */
    @GetMapping("/get/image")
    public RespMessage getImg(String imgCode, Integer width, Integer quality, HttpServletResponse response) throws IOException {
        if (StringUtils.isEmpty( imgCode )) {
            return RespMessage.error( "参数不完整" );
        }
        width = width == null ? 500 : width;
        quality = quality == null ? 100 : width;
        byte[] img = api.getImg( imgCode, width, quality );
        response.setContentType( "image/png" );
        OutputStream os = response.getOutputStream();
        os.write( img );
        os.flush();
        os.close();
        return null;
    }

    /**
     * @api {get} /api/kaola/get/goods/info 获取商品详情
     * @apiVersion 1.0.0
     * @apiName goods
     * @apiGroup kaola
     * @apiParam {int} goodsId 商品id
     * @apiSuccess {Number} status 状态码,0为接口访问成功
     * @apiSuccess {String} err 错误信息
     * @apiSuccess {Object} datas 成功时返回考拉接口数据
     */
    @GetMapping("/get/goods/info")
    public RespMessage getGoodsInfo(Long goodsId) {
        if (goodsId == null) {
            return RespMessage.error( "id不能为空" );
        }
        return api.getGoodsInfo( goodsId );
    }

    /**
     * @api {get} /api/kaola/get/goods/reviews 获取商品评论
     * @apiVersion 1.0.0
     * @apiName reviews
     * @apiGroup kaola
     * @apiParam {int} goodsId 商品id
     * @apiSuccess {Number} status 状态码,0为接口访问成功
     * @apiSuccess {String} err 错误信息
     * @apiSuccess {Object} datas 成功时返回考拉接口数据
     */
    @GetMapping("/get/goods/reviews")
    public RespMessage reviews(Long goodsId) {
        if (goodsId == null) {
            return RespMessage.error( "id不能为空" );
        }
        return api.getGoodsReviews( goodsId );
    }
}
